package com.shoplink.launcher;

import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.net.NetworkInfo.DetailedState;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class WifiAdapter extends BaseAdapter {
	private final static String TAG = "WifiAdapter ";

	private Context mContext;

	private LayoutInflater mInflater;

	private List<ScanResult> mScanResultList;

	private List<AccessPoint> accessPoints;

	public interface IonPswEorrListener {
		public void onErorr(int netWorkId);
	}

	private IonPswEorrListener eorrListener;

	public void setOnPswErorrListener(IonPswEorrListener eorrListener) {
		this.eorrListener = eorrListener;
	}

	public WifiAdapter(Context context, List<AccessPoint> accessPoints) {
		mContext = context;
		this.accessPoints = accessPoints;
		// mWifiManager = (WifiManager) mContext
		// .getSystemService(Context.WIFI_SERVICE);
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// mScanResultList = new ArrayList<ScanResult>();
	}

	class sortByLevel implements Comparator<ScanResult> {
		public int compare(ScanResult obj1, ScanResult obj2) {
			if (obj1.level > obj2.level)
				return 1;
			else
				return 0;
		}
	}

	@Override
	public int getCount() {
		return accessPoints.size();
	}

	public void refreshItem(AccessPoint accessPoint) {

		for (int i = 0; i < accessPoints.size(); i++) {
			if (accessPoints.get(i).ssid.equals(accessPoint.ssid)) {
				accessPoints.get(i).setAllResult(accessPoint.mScanResult,
						accessPoint.getConfig(), accessPoint.networkId);
				if (accessPoints.get(i).getState() == DetailedState.DISCONNECTED) {
					accessPoints.get(i).setWifiState(null);
				}
				break;
			}
		}
		this.notifyDataSetChanged();
	}

	@Override
	public AccessPoint getItem(int index) {
		return accessPoints.get(index);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	private class AccessPointItemHolder {
		ImageView wifi_selected;
		ImageView wifi_level;
		ImageView wifi_lock;
		TextView wifi_ap_name;
		TextView wifi_state;
	}

	@Override
	public View getView(int index, View convertView, ViewGroup parent) {

		if (index == (accessPoints.size() - 1)) {
			convertView = mInflater.inflate(R.layout.add_ap_layout, null);
			return convertView;
		} else {
			AccessPointItemHolder holder = null;

			if (convertView == null
					|| convertView.getId() != R.layout.preference_wifi_ap) {
				holder = new AccessPointItemHolder();
				convertView = mInflater.inflate(R.layout.preference_wifi_ap,
						null);
				convertView.setTag(holder);
			} else {
				holder = (AccessPointItemHolder) convertView.getTag();
			}

			holder.wifi_selected = (ImageView) convertView
					.findViewById(R.id.wifi_ap_status);
			holder.wifi_lock = (ImageView) convertView
					.findViewById(R.id.wifi_ap_secured);
			holder.wifi_level = (ImageView) convertView
					.findViewById(R.id.wifi_ap_level);
			holder.wifi_ap_name = (TextView) convertView
					.findViewById(R.id.wifi_ap_title);
			holder.wifi_state = (TextView) convertView
					.findViewById(R.id.wifi_ap_summary);
			AccessPoint accessPoint = accessPoints.get(index);
			DetailedState state = accessPoint.getState();
			boolean isConnect = accessPoint.getConnected();
			if (isConnect) {
				holder.wifi_selected.setVisibility(View.INVISIBLE);
				holder.wifi_state.setVisibility(View.VISIBLE);
				holder.wifi_state.setText(mContext
						.getString(R.string.wifi_connect_now) + "...");
			} else {
				if (state == null) {
					holder.wifi_state.setText("");
				}
			}
			if (state != null) {
				switch (state) {
				case OBTAINING_IPADDR:
					accessPoint.setConnecting(false);
					holder.wifi_state.setVisibility(View.VISIBLE);
					if (accessPoint.security != 0) {
						holder.wifi_state.setText(mContext
								.getString(R.string.wifi_password_failure)
								+ "...");
					} else {
						holder.wifi_state.setText(mContext
								.getString(R.string.wifi_getipads_now) + "...");
					}
					break;
				case CAPTIVE_PORTAL_CHECK:
				case VERIFYING_POOR_LINK:
					holder.wifi_state.setText(mContext
							.getString(R.string.wifi_getipads_now) + "...");
					break;
				case IDLE:
				case SCANNING:
				case CONNECTING:
				case AUTHENTICATING:
				case DISCONNECTED:
					// holder.wifi_state.setVisibility(View.INVISIBLE);
					// status.setVisibility(View.INVISIBLE);
					break;
				case CONNECTED:
					holder.wifi_state.setVisibility(View.INVISIBLE);
					holder.wifi_selected.setVisibility(View.VISIBLE);
					holder.wifi_selected
							.setImageResource(R.drawable.ico_select);
					break;
				case SUSPENDED:
				case DISCONNECTING:
				case FAILED:
				case BLOCKED:
					accessPoint.setConnecting(false);
					// holder.wifi_state.setVisibility(View.VISIBLE);
					// holder.wifi_selected.setVisibility(View.VISIBLE);
					// holder.wifi_state.setText("连接失败");
					// holder.wifi_selected.setImageResource(R.drawable.ico_error);
					// System.out.println("======================d");
					break;

				default:
					holder.wifi_selected.setVisibility(View.INVISIBLE);
					break;
				}

				if (state.ordinal() == 8) {
					holder.wifi_state.setText(Summary.get(mContext, state));
					if (state.ordinal() == 8) {
						if (eorrListener != null) {
							eorrListener.onErorr(accessPoint.networkId);
						}
						holder.wifi_state.setVisibility(View.VISIBLE);
						holder.wifi_selected.setVisibility(View.VISIBLE);
						holder.wifi_selected
								.setImageResource(R.drawable.ico_error);
						holder.wifi_state.setText(R.string.wifi_adjust_erorr);

					}

				}
//				if (TextUtils.isEmpty(holder.wifi_state.getText())) {
//					holder.wifi_state.setText(" " + state.ordinal());
//				}

			} else {
				holder.wifi_selected.setVisibility(View.INVISIBLE);
				WifiConfiguration mConfig = accessPoint.getConfig();
				if (mConfig != null) {
					if (mConfig.disableReason == 3 && isConnect) {

						// accessPoints.get(index).setConnecting(false);
						holder.wifi_state.setVisibility(View.VISIBLE);
						holder.wifi_selected.setVisibility(View.VISIBLE);
						holder.wifi_selected
								.setImageResource(R.drawable.ico_error);
						holder.wifi_state.setText(R.string.wifi_adjust_erorr);
						System.out.println("======================c");
						if (eorrListener != null) {
							eorrListener.onErorr(accessPoint.networkId);
						}
					}

				} else if (!isConnect) {
					holder.wifi_state.setVisibility(View.VISIBLE);
					holder.wifi_selected.setVisibility(View.INVISIBLE);
					holder.wifi_state.setText("");
				}
			}

			holder.wifi_ap_name.setText(accessPoint.ssid);
			int level = accessPoint.getLevel();
			holder.wifi_level.setImageLevel(level);
			holder.wifi_level.setImageResource(R.drawable.wifi_signal);

			if (accessPoint.security == 0) {
				holder.wifi_lock.setVisibility(View.INVISIBLE);
			} else {
				holder.wifi_lock.setVisibility(View.VISIBLE);
			}
			return convertView;
		}
	}
}